/**
 * Copyright (c) 2018, wuxie All rights reserved.
 * qq:16349023,mail:16349023@qq.com
 * 未经许可禁止任何人通过任何渠道使用、修改源代码.
 * 项目名称 : wxcloud
 *
 * @version V1.0
 */
package cn.xo68.boot.auth.core.configuration;


import cn.xo68.boot.auth.core.cookie.MapCookieTemplate;
import cn.xo68.boot.auth.core.cookie.StringCookieTemplate;
import cn.xo68.boot.auth.core.properties.OAuthResourceProperties;
import cn.xo68.core.util.JsonUtil;
import cn.xo68.core.util.StringTools;
import org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *  cookie配置
 * @author wuxie
 * @date 2018/8/5 18:42
 *
 */
@Configuration
public class CookieConfiguration {

    /**
     * session id 在cookie中的键
     */
    public static final String SESSION_COOKIE_KEY="wxsid";

    @Bean
    public JavaUuidSessionIdGenerator sessionIdGenerator(){
        return new JavaUuidSessionIdGenerator();
    }

    @Bean("sessionIdCookie")
    public SimpleCookie sessionIdCookie(){
        SimpleCookie simpleCookie=new SimpleCookie();
        simpleCookie.setName(SESSION_COOKIE_KEY);
        simpleCookie.setHttpOnly(true);
        simpleCookie.setMaxAge(60*60);
        simpleCookie.setPath("/");

        return simpleCookie;
    }

    @Bean("accessTokenCookie")
    public SimpleCookie accessTokenCookie(OAuthResourceProperties oAuthResourceProperties){
        SimpleCookie simpleCookie=new SimpleCookie();
        simpleCookie.setName(oAuthResourceProperties.getAccessTokenCookieName());
        simpleCookie.setHttpOnly(false);
        simpleCookie.setPath("/");
        simpleCookie.setMaxAge(oAuthResourceProperties.getTokenExpireSeconds());
        if(StringTools.isNotEmpty(oAuthResourceProperties.getCookieDomain())){
            simpleCookie.setDomain(oAuthResourceProperties.getCookieDomain());
        }

        return simpleCookie;
    }

    @Bean("browerDataCookieTemplate")
    public MapCookieTemplate browerDataCookieTemplate(JsonUtil jsonUtil,OAuthResourceProperties oAuthResourceProperties){
        MapCookieTemplate mapCookieTemplate=new MapCookieTemplate(jsonUtil,oAuthResourceProperties.getBrowerDataCookieName());
        //保持60分钟
        mapCookieTemplate.setMaxAge(60*60);
        if(StringTools.isNotEmpty(oAuthResourceProperties.getCookieDomain())){
            mapCookieTemplate.setDomain(oAuthResourceProperties.getCookieDomain());
        }
        return mapCookieTemplate;
    }

    @Bean("simpleCookieTemplate")
    public StringCookieTemplate simpleCookieTemplate(JsonUtil jsonUtil, OAuthResourceProperties oAuthResourceProperties){
        StringCookieTemplate cookieTemplate=new StringCookieTemplate(jsonUtil,"simpleCookie");
        //保持60分钟
        cookieTemplate.setMaxAge(60*60);
        if(StringTools.isNotEmpty(oAuthResourceProperties.getCookieDomain())){
            cookieTemplate.setDomain(oAuthResourceProperties.getCookieDomain());
        }
        return cookieTemplate;
    }

    @Bean("clientPrincipalCookieTemplate")
    public StringCookieTemplate clientPrincipalCookieTemplate(JsonUtil jsonUtil, OAuthResourceProperties oAuthResourceProperties){
        StringCookieTemplate cookieTemplate=new StringCookieTemplate(jsonUtil,"clientPrincipal");
        //保持一个月
        cookieTemplate.setMaxAge(60*60*24*30);
        if(StringTools.isNotEmpty(oAuthResourceProperties.getCookieDomain())){
            cookieTemplate.setDomain(oAuthResourceProperties.getCookieDomain());
        }
        return cookieTemplate;
    }
}
